<%@ page language="java" import="java.util.*" pageEncoding="UTF8"%>
<%@page import="Annotation.DataBase.*"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String act=request.getParameter("act");//input hidden

if(act != null && act.equals("check")){

	String schoolname = request.getParameter("schoolname");//input text
	boolean adding=true;
	
	MySQLCommand ss= new MySQLCommand();
	String sql="select school from school";
	ResultSet rs = ss.selectSQL(sql);
	while(rs.next()){
		String Sch=rs.getString("school");
		if(schoolname.equals(Sch)){
			//System.out.println("have same school name");
			out.print("<script>alert('have same school name');</script>");
			adding=false;
		}
		//adding=true;
	}
	ss.closeDB();
	
	if(adding==true){
		sql="insert into school (school) values ('" + schoolname +"')";
		ss.executeSQL(sql);
		out.print("<script>alert('adding school name is succeful!');</script>");		
	}
}

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'addSchoolName.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<div align="center"> 
    		Adding School Name into Database 
    		<form name="createAccForm" id="createAccForm" method="post" action="addSchoolName.jsp">
	    		<table border="0" width="50%">
	    		
	    			<tr>
	    				<td width="40%" align="left">School name</td>
	    				<td width="60%" align="center">
	    					<input type="text" id="schoolname" name="schoolname" width="100%" /> 
	    				</td>
	    			</tr>
	    			
	    			<tr>
	    				<td colspan="2" align="center">	    					
	    					<font color="red">
		    					<div id="alertlabel" style="display:none">
		    					</div>
	    					</font>
	    					
	    				</td>
	    			</tr>
	    			<tr>
	    				<td colspan="2" align="center">
	    					<input type="submit" value="CREATE" />
	    				</td>
	    			</tr>		
	    		</table>
				<input type="hidden" id="act" name="act" value="check" />
    		</form>
    	</div>
  </body>
</html>
